package gwtmodule1.client.gui.shared;

import gwtmodule1.client.gui.utils.Currency;
import gwtmodule1.client.gui.utils.DekoTable;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.TextBox;

public class CartTable extends DekoTable {

	private final static int COL_BRAND = 0;

	private final static int COL_PRODUCT = 1;

	private final static int COL_PRICEPP = 2;

	private final static int COL_AMMOUNT = 3;

	private final static int COL_PRICE = 4;

	private final static int COL_DEL = 5;

	public CartTable() {
		super();
		FlexCellFormatter cf = (FlexCellFormatter) getCellFormatter();
		setText(0, COL_BRAND, "Brand");
		setText(0, COL_PRODUCT, "Product");
		setText(0, COL_PRICEPP, "Price/#");
		setText(0, COL_AMMOUNT, "#");
		setText(0, COL_PRICE, "Price");
		setText(0, COL_DEL, "");
		setText(1, 0, "Gift Card");
		setText(1, 1, "USD");
		setText(1, 2, "");
		setText(2, 0, "Shipping");
		setText(2, 1, "USD");
		setText(2, 2, "");
		setText(3, 0, "Tax (0.0%)");
		setText(3, 1, "USD");
		setText(3, 2, "");
		setText(4, 0, "Total");
		setText(4, 1, "USD");
		setText(4, 2, "");
		cf.setColSpan(1, 0, 4);
		cf.setColSpan(2, 0, 4);
		cf.setColSpan(3, 0, 4);
		cf.setColSpan(4, 0, 4);
		cf.setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_RIGHT);
		cf.setHorizontalAlignment(2, 1, HasHorizontalAlignment.ALIGN_RIGHT);
		cf.setHorizontalAlignment(3, 1, HasHorizontalAlignment.ALIGN_RIGHT);
		cf.setHorizontalAlignment(4, 1, HasHorizontalAlignment.ALIGN_RIGHT);
	}

	public float addItem(String brand, String product, float pricepp,
			int ammount) {

		int i = insertRow(getRowCount() - 4);

		TextBox tb = new TextBox();
		tb.setText(ammount + "");
		tb.setWidth("32px");
		tb.setTextAlignment(TextBox.ALIGN_RIGHT);

		CellFormatter cf = getCellFormatter();
		cf.setHorizontalAlignment(i, COL_PRICEPP,
				HasHorizontalAlignment.ALIGN_RIGHT);
		cf.setHorizontalAlignment(i, COL_AMMOUNT,
				HasHorizontalAlignment.ALIGN_RIGHT);
		cf.setHorizontalAlignment(i, COL_PRICE,
				HasHorizontalAlignment.ALIGN_RIGHT);
		cf.setHorizontalAlignment(i, COL_DEL,
				HasHorizontalAlignment.ALIGN_CENTER);

		setText(i, COL_BRAND, brand);
		setText(i, COL_PRODUCT, product);
		setText(i, COL_PRICEPP, Currency.format(pricepp));
		setWidget(i, COL_AMMOUNT, tb);
		setText(i, COL_PRICE, Currency.format(pricepp * ammount));
		setWidget(i, COL_DEL, new Button("delete"));
		return (pricepp * ammount);
	}

	public void setGiftcard(float ammount) {
		int i = getRowCount() - 4;
		setText(i, 1, "-" + Currency.format(ammount));
	}

	public void setShipping(float ammount) {
		int i = getRowCount() - 3;
		setText(i, 1, Currency.format(ammount));
	}

	public void setTax(double percent, float ammount) {
		int i = getRowCount() - 2;
		setText(i, 0, "Tax (" + percent + "%)");
		setText(i, 1, Currency.format(ammount));
	}

	public void setTotal(float ammount) {
		int i = getRowCount() - 1;
		setHTML(i, 1, "<b>" + Currency.format(ammount) + "</b>");
	}

}
